package com.hancai.web;

import com.github.pagehelper.PageInfo;
import com.hancai.model.TClient;
import com.hancai.query.BarrageAndClientStatusQuery;
import com.hancai.query.ClientQuery;
import com.hancai.result.R;
import com.hancai.service.ClientService;
import jakarta.annotation.Resource;
import org.springframework.web.bind.annotation.*;



/**
 * @author 涵菜
 * @version 1.0
 */
@RestController
public class ClientController {
    @Resource
    private ClientService clientService;

    @GetMapping(value="/api/clients")
    public R clientPage(@RequestParam(value = "current",required = false)Integer current ,@RequestParam(value = "keyword",required = false)String keyword){
        if(current==null){
            current=1;
        }
        PageInfo<TClient> pageInfo = clientService.getClientByPage(current,keyword);
        return R.OK(pageInfo);
    }


    /**
     * 新增用户
     * @param clientQuery
     * @return
     */
    @PostMapping(value="/api/client")
    public R addClient(ClientQuery clientQuery, @RequestHeader(value = "Authorization")String token){
        //把前端请求头的token放入clientQuery里面
        clientQuery.setToken(token);
        int save=clientService.saveClient(clientQuery);
        return save>=1 ? R.OK() : R.FAIL();
    }

    /**
     * 编辑用户
     * @param clientQuery
     * @return
     */
    @PutMapping(value="/api/client")
    public R editClient(ClientQuery clientQuery,@RequestHeader(value = "Authorization")String token){
        //把前端请求头的token放入clientQuery里面
        clientQuery.setToken(token);
        int update=clientService.updateClient(clientQuery);
        return update>=1 ? R.OK() : R.FAIL();
    }



    /**
     * 跟据id删除客户
     * @param id
     * @return
     */
    @DeleteMapping(value = "/api/client/{id}")
    public R delClient(@PathVariable(value="id")Integer id){
        int del=clientService.delClientById(id);
        return del>=1 ? R.OK() : R.FAIL();
    }


    /**
     * 修改客户状态
     * @return
     */
    @PutMapping(value = "/api/client/changeStatus")
    public R changeStatus(@RequestBody BarrageAndClientStatusQuery barrageAndClientStatusQuery){
        // 获取参数
        Integer id = barrageAndClientStatusQuery.getId();
        Integer status = barrageAndClientStatusQuery.getStatus();
        clientService.changeStatus(id,status);
        return R.OK();
    }


    /**
     * 加载用户详情页（用于编辑）
     * @param id
     * @return
     */
    @GetMapping(value = "/api/client/{id}")
    public R clientDetail(@PathVariable(value = "id")Integer id){
        TClient tClient=clientService.getClientById(id);
        return R.OK(tClient);
    }
}
